import { createRouter, createWebHistory } from 'vue-router';

import { fRequest } from '../api/request.js'
import useStoreIndex from '../store/modules/index';
import pinia from '../store/store';
import { ElLoading } from 'element-plus';

const router = createRouter({
    history: createWebHistory(),
    routes: [],
});
let oStore = useStoreIndex(pinia);//
let aRouters = [];

let bShowLoading = null;
const fLoading = () => {
    bShowLoading = ElLoading.service({
        target: '#app',
        lock: true,
        text: 'Loading',
        body: true,
        background: 'rgba(0, 0, 0, 0.7)',
    })
}
fLoading();
// 获取路由列表
let modules = import.meta.glob('../pages/**/*.vue')
const fGetRouterList = (data) => {
    data.forEach(item => {
        if (item.children && item.children.length > 0) {
            fGetRouterList(item.children);
        } else {
            if (item.components) {
                aRouters.push({
                    path: item.path,
                    name: item.path,
                    component: modules[`../${item.components}.vue`]
                });
            }
        }
    });
};
// 请求路由
const fRequestMenuTree = () => {
    fRequest({
        url: '/api/getMenuTree',
        method: 'get',
    }).then(res => {
        console.log(res)
        oStore.fSetMuenList(res.data.menulist);
        fGetRouterList(res.data.menulist);
        bShowLoading.close();

    })
}

fRequestMenuTree();

router.addRoute([...aRouters])

export default router;